import request from '@/utils/request'

export interface Message {
  type: 'text' | 'image'
  content: string
  isUser: boolean
  isExpanded?: boolean
}

export interface Model {
  name: string
  displayName: string
  supported_methods: string[]
}

export interface ValidateKeyResponse {
  success: boolean
  models?: Model[]
  error?: string
  data?: any
}

export interface SendMessageParams {
  message: string
  model: string
}

export interface SendMessageResponse {
  data?: {
    success: boolean
    message?: Message
    error?: string
  }
}

export interface ConfigResponse {
  success: boolean
  config?: {
    api_key: string
    current_model: string
  }
  error?: string
  data?: any
}

export interface HistoryResponse {
  success: boolean
  history?: Array<{
    role: string
    content: string
  }>
  error?: string
  data?: any
}

export interface DeleteMessageResponse {
  success: boolean
  message?: string
  error?: string
}

export interface SetModelRequest {
  model: string
}

export interface SetModelResponse {
  data: {
    message?: string
    error?: string
    success?: boolean
  }
}

export const chatApi = {
  async validateKey(apiKey: string): Promise<ValidateKeyResponse> {
    return await request.post<ValidateKeyResponse>('/validate_key', { apiKey })
  },

  async deleteMessage(index: number): Promise<DeleteMessageResponse> {
    return await request.post<DeleteMessageResponse>('/delete_message', { index })
  },

  async getConfig(): Promise<ConfigResponse> {
    return await request.get<ConfigResponse>('/config')
  },

  async getHistory(): Promise<HistoryResponse> {
    return await request.get<HistoryResponse>('/history')
  },

  async setModel(params: SetModelRequest): Promise<SetModelResponse> {
    return await request.post<SetModelResponse>('/set_model', params)
  },

  async clearHistory(): Promise<SetModelResponse> {
    return request.post<SetModelResponse>('/clear_history')
  },

  async chatStoryboard(params: SendMessageParams): Promise<SendMessageResponse> {
    return await request.post<SendMessageResponse>('/chat_storyboard', params)
  }
}
